package com.cldz.controller;

import com.cldz.common.dto.req.DevAlarmDto;
import com.cldz.common.dto.res.DeviceAlarmResDto;
import com.cldz.service.DevAlarmService;
import com.cldz.dao.UserPropertyMapper;
import com.cldz.feign.DevAlarmFeign;
import com.cldz.response.support.ServerResponse;
import com.cldz.utils.MathUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 15:44 2018/12/14
 * @ Description：获取设备告警类接口
 * @ Modified By：
 */
@Slf4j
@RestController
public class DevAlarmController implements DevAlarmFeign {


    @Autowired
    private DevAlarmService devAlarmService;

    @Autowired
    private UserPropertyMapper userPropertyMapper;

    /**
     * 获取全部设备实时告警
     * @param devAlarmDto
     * @return
     */
    @GetMapping("/runAlarmData")
    public ServerResponse getRunAlarmData(DevAlarmDto devAlarmDto){
        Integer areaID = devAlarmDto.getAreaID();

        if(areaID == null){
            areaID = userPropertyMapper.queryAreaIDByUserID(devAlarmDto.getUserID());

        }
        devAlarmDto.setAreaID(MathUtils.getAreaIDPrefix(areaID));

        List<DeviceAlarmResDto> runAlarmData = devAlarmService.getRunAlarmData(devAlarmDto);
        Integer amount = devAlarmService.getRunAlarmDataAmount(devAlarmDto);
        return ServerResponse.createDataAndCount(amount,runAlarmData);
    }

    /**
     * 获取指定设备实时告警
     * @param devAlarmDto
     * @return
     */
    @GetMapping("/runDeviceAlarmData")
    public ServerResponse getRunDeviceAlarmData(DevAlarmDto devAlarmDto){
        List<DeviceAlarmResDto> data = devAlarmService.getRunDeviceAlarmData(devAlarmDto);
        Integer amount = devAlarmService.getRunDeviceAlarmDataAmount(devAlarmDto);
        return ServerResponse.createDataAndCount(amount,data);
    }

    /**
     * 获取全部设备历史告警
     * @param devAlarmDto
     * @return
     */
    @GetMapping("/hisAlarmData")
    public ServerResponse getHisAlarmData(DevAlarmDto devAlarmDto){
        Integer areaID = devAlarmDto.getAreaID();

        if(areaID == null){
            areaID = userPropertyMapper.queryAreaIDByUserID(devAlarmDto.getUserID());

        }
        devAlarmDto.setAreaID(MathUtils.getAreaIDPrefix(areaID));

        List<DeviceAlarmResDto> data = devAlarmService.getHisAlarmData(devAlarmDto);
        Integer amount = devAlarmService.getHisAlarmDataAmount(devAlarmDto);
        return ServerResponse.createDataAndCount(amount,data);
    }


    /**
     * 获取指定设备历史告警
     * @param devAlarmDto
     * @return
     */
    @GetMapping("/hisDeviceAlarmData")
    public ServerResponse getHisDeviceAlarmData(DevAlarmDto devAlarmDto){
        List<DeviceAlarmResDto> data = devAlarmService.getHisDeviceAlarmData(devAlarmDto);
        Integer amount = devAlarmService.getHisDeviceAlarmDataAmount(devAlarmDto);
        return ServerResponse.createDataAndCount(amount,data);
    }

    /**
     * 获取多个设备告警数量
     * @param devAlarmDto
     * @return
     */
    @GetMapping("/devsRunAlarmCount")
    public ServerResponse getDevsRunAlarmCount(DevAlarmDto devAlarmDto){
        String[] split = StringUtils.split(devAlarmDto.getStrDevs(), ",");
        List<Map<String,Object>> list = devAlarmService.getDevsRunAlarmCount(split);
        return ServerResponse.createDataList(list);
    }

    /**
     * 获取告警信息
     * @param devAlarmDto
     * @return
     */
    @GetMapping("/alarmMsgs")
    public ServerResponse getAlarmMsgs(DevAlarmDto devAlarmDto){
        List<DeviceAlarmResDto> alarmMsgs = devAlarmService.getAlarmMsgs(devAlarmDto);
        Integer alarmMsgsAmount = devAlarmService.getAlarmMsgsAmount(devAlarmDto);
        return ServerResponse.createDataAndCount(alarmMsgsAmount,alarmMsgs);
    }



}
